package com.kurumi.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.kurumi.mapper.MedicalRecordMapper;
import com.kurumi.mapper.MedicalRecordTraceMapper;
import com.kurumi.pojo.MedicalRecordPageTrace;
import com.kurumi.pojo.MedicalRecordTrace;
import com.kurumi.query.MedicalRecordQuery;
import com.kurumi.service.MedicalRecordTraceService;

@Service
public class MedicalRecordTraceServiceImpl implements MedicalRecordTraceService{

	@Autowired
	private MedicalRecordMapper medicalRecordMapper;
	
	@Autowired
	private MedicalRecordTraceMapper medicalRecordTraceMapper;
	
	@Override
	public int insertMedicalRecordTrace(MedicalRecordTrace medicalRecordTrace) {
		// TODO Auto-generated method stub
		Subject subject=SecurityUtils.getSubject();
		Session session = subject.getSession();
		Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
		medicalRecordTrace.setCreateUserId((String)currentUser.get("user_code"));
		medicalRecordTrace.setCreateUserName((String)currentUser.get("user_name"));
		return medicalRecordMapper.insertMedicalRecordTrace(medicalRecordTrace);
	}

	@Override
	public List<Map<String, Object>> getMeditalRecordPageTraceByVisitGuid(String visitGuid) {
		// TODO Auto-generated method stub
		return medicalRecordTraceMapper.getMeditalRecordPageTraceByVisitGuid(visitGuid);
	}

	@Override
	public int insertMedicalRecordPageTrace(MedicalRecordPageTrace medicalRecordPageTrace) {
		// TODO Auto-generated method stub
		return medicalRecordTraceMapper.insertMedicalRecordPageTrace(medicalRecordPageTrace);
	}

	@Override
	public List<Map<String, Object>> getMedicalRecordOfPagePutAwayTrace(MedicalRecordQuery medicalRecordQuery) {
		// TODO Auto-generated method stub
		return medicalRecordTraceMapper.getMedicalRecordOfPagePutAwayTrace(medicalRecordQuery);
	}

	@Override
	public int getMedicalRecordCountOfPagePutAwayTrace(MedicalRecordQuery medicalRecordQuery) {
		// TODO Auto-generated method stub
		return medicalRecordTraceMapper.getMedicalRecordCountOfPagePutAwayTrace(medicalRecordQuery);
	}

	@Override
	public List<Map<String, Object>> getMedicalRecordOfPageTrace(MedicalRecordQuery medicalRecordQuery) {
		// TODO Auto-generated method stub
		return medicalRecordTraceMapper.getMedicalRecordOfPageTrace(medicalRecordQuery);
	}

	@Override
	public int getMedicalRecordCountOfPageTrace(MedicalRecordQuery medicalRecordQuery) {
		// TODO Auto-generated method stub
		return medicalRecordTraceMapper.getMedicalRecordCountOfPageTrace(medicalRecordQuery);
	}

	@Override
	public List<Map<String, Object>> getMedicalRecordOfSigned(MedicalRecordQuery medicalRecordQuery) {
		// TODO Auto-generated method stub
		return medicalRecordTraceMapper.getMedicalRecordOfSigned(medicalRecordQuery);
	}

	@Override
	public int getMedicalRecordCountOfSigned(MedicalRecordQuery medicalRecordQuery) {
		// TODO Auto-generated method stub
		return medicalRecordTraceMapper.getMedicalRecordCountOfSigned(medicalRecordQuery);
	}

	@Override
	public List<Map<String, Object>> getMedicalRecordOfScan(MedicalRecordQuery medicalRecordQuery) {
		// TODO Auto-generated method stub
		return medicalRecordTraceMapper.getMedicalRecordOfScan(medicalRecordQuery);
	}

	@Override
	public int getMedicalRecordCountOfScan(MedicalRecordQuery medicalRecordQuery) {
		// TODO Auto-generated method stub
		return medicalRecordTraceMapper.getMedicalRecordCountOfScan(medicalRecordQuery);
	}

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int insertQualityControlPageTrace(String visitGuid) {
		// TODO Auto-generated method stub
		int count = medicalRecordTraceMapper.getCountByVisitGuidAndPageTraceTypeCode(visitGuid, MedicalRecordPageTrace.VISIT_QUALITY);
		if(count > 0){
			return 1;
		}
		/*List<String> pageTraceCodes = medicalRecordTraceMapper.getLastPageTraceByVisitGuid(visitGuid);
		if(!pageTraceCodes.isEmpty()){
			if(pageTraceCodes.get(0).equalsIgnoreCase(MedicalRecordPageTrace.VISIT_QUALITY)){
				return 1;
			}
			
		}*/
		Subject subject=SecurityUtils.getSubject();
		Session session = subject.getSession();
		Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
		String userCode =(String)currentUser.get("user_code");
		String userName =(String)currentUser.get("user_name");
		MedicalRecordPageTrace putwayPageTrace = new MedicalRecordPageTrace();
		
		putwayPageTrace.setVisitGuid(visitGuid);
		putwayPageTrace.setCreateUserId(userCode);
		putwayPageTrace.setCreateUserName(userName);
		putwayPageTrace.setTraceTypeCode(MedicalRecordPageTrace.VISIT_QUALITY);
		putwayPageTrace.setTraceTypeName(MedicalRecordPageTrace.VISIT_QUALITY_NAME);
		putwayPageTrace.setTraceDateTime(new Date());
		return medicalRecordTraceMapper.insertMedicalRecordPageTrace(putwayPageTrace);
		
	}

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int insertCodingPageTrace(String visitGuid) {
		// TODO Auto-generated method stub
		int count = medicalRecordTraceMapper.getCountByVisitGuidAndPageTraceTypeCode(visitGuid, MedicalRecordPageTrace.VISIT_CODING);
		if(count > 0){
			return 1;
		}
		/*List<String> pageTraceCodes = medicalRecordTraceMapper.getLastPageTraceByVisitGuid(visitGuid);
		if(!pageTraceCodes.isEmpty()){
			if(pageTraceCodes.get(0).equalsIgnoreCase(MedicalRecordPageTrace.VISIT_QUALITY)){
				return 1;
			}
			
		}*/
		Subject subject=SecurityUtils.getSubject();
		Session session = subject.getSession();
		Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
		String userCode =(String)currentUser.get("user_code");
		String userName =(String)currentUser.get("user_name");
		MedicalRecordPageTrace putwayPageTrace = new MedicalRecordPageTrace();
		
		putwayPageTrace.setVisitGuid(visitGuid);
		putwayPageTrace.setCreateUserId(userCode);
		putwayPageTrace.setCreateUserName(userName);
		putwayPageTrace.setTraceTypeCode(MedicalRecordPageTrace.VISIT_CODING);
		putwayPageTrace.setTraceTypeName(MedicalRecordPageTrace.VISIT_CODING_NAME);
		putwayPageTrace.setTraceDateTime(new Date());
		return medicalRecordTraceMapper.insertMedicalRecordPageTrace(putwayPageTrace);
	}


}
